package com.chain.BrotherExchange.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.MenuItem;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.event.AlertLanguage;
import com.chain.BrotherExchange.event.LogoutEvent;
import com.chain.BrotherExchange.ui.core.BaseActivity;
import com.chain.BrotherExchange.ui.fragment.CoinFragment;
import com.chain.BrotherExchange.ui.fragment.HomeFragment;
import com.chain.BrotherExchange.ui.fragment.LegalTenderFragment;
import com.chain.BrotherExchange.ui.fragment.MyFragment;
import com.chain.BrotherExchange.ui.fragment.QuotationFragment;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.viewmodel.MainViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;
import com.example.common.update.AppDialogDownloadView;

import org.greenrobot.eventbus.Subscribe;

import java.util.Map;
import java.util.Set;

import butterknife.Bind;

public class MainActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    @Bind(R.id.bottom_navigation)
    public BottomNavigationView mBottomNavigationView;

    private Map<Integer, Fragment> fragmentMap = Androids.getMap();

    public static void launch(Context context) {
        context.startActivity(new Intent(context, MainActivity.class));
    }

    public static void launchToCoin(Context context) {
        context.startActivity(new Intent(context, MainActivity.class)
                .putExtra(C.EXTRA_LAUNCH_PAGE, C.VALUE_COIN_PAGE));
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        // 启动币币页面
        if (intent != null && mBottomNavigationView != null){
            int launchPage = intent.getIntExtra(C.EXTRA_LAUNCH_PAGE, -1);
            if (launchPage == C.VALUE_COIN_PAGE){
                mBottomNavigationView.setSelectedItemId(R.id.nav_coin);
            }
        }
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    protected void initActions() {
        // 添加Fragment
        fragmentMap.put(R.id.nav_home, HomeFragment.newInstance());
        fragmentMap.put(R.id.nav_quotation, QuotationFragment.newInstance());
        fragmentMap.put(R.id.nav_legal_tender, LegalTenderFragment.newInstance());
        fragmentMap.put(R.id.nav_coin, CoinFragment.newInstance());
        fragmentMap.put(R.id.nav_my, MyFragment.newInstance());

        mBottomNavigationView.setOnNavigationItemSelectedListener(this);
        mBottomNavigationView.setSelectedItemId(R.id.nav_home);

        // 检查新版本
        MainViewModel mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
        mainViewModel.updateInfo().observe(this, updateInfo ->
                AppDialogDownloadView.show(MainActivity.this, updateInfo, false)
        );
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment targetFragment = fragmentMap.get(item.getItemId());

        if (targetFragment != null) {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            if (!targetFragment.isAdded()) {
                fragmentTransaction.add(R.id.content, targetFragment);
            }

            Set<Map.Entry<Integer, Fragment>> entries = fragmentMap.entrySet();
            for (Map.Entry<Integer, Fragment> fragmentEntry : entries) {
                Fragment fragmentEntryValue = fragmentEntry.getValue();
                if (fragmentEntryValue == targetFragment) {
                    fragmentTransaction.show(fragmentEntryValue);
                } else {
                    fragmentTransaction.hide(fragmentEntryValue);
                }
            }

            fragmentTransaction.commit();
            return true;
        }

        return false;
    }

    @Override
    public void onBackPressed() {
        if (Androids.doubleClickClose(R.string.double_click_exit_application)) {
            super.onBackPressed();
        }
    }

    @SuppressLint("MissingSuperCall")
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // super.onSaveInstanceState(outState);
        // 解决系统回收Activity，Fragment叠加问题
    }

    @Subscribe
    public void logoutEvent(LogoutEvent event) {
        mBottomNavigationView.postDelayed(() ->
                mBottomNavigationView.setSelectedItemId(R.id.nav_home), 200
        );
    }

    @Subscribe
    public void alertLanguage(AlertLanguage alertLanguage){
        Androids.alertLanguage(this, alertLanguage.language);
    }
}
